package vehement.game.nodes;


//import javax.microedition.khronos.opengles.GL10;
//import javax.vecmath.Vector3f;

import java.io.IOException;

import org.lwjgl.opengl.GL11;

import vehement.game.TerrainType;
import vehement.game.graphics.Ob;
import vehement.game.graphics.primitive.Texture;
import vehement.game.interfaces.Drawable;

public class TileNode implements Drawable{

	//TODO: there should be a central place to store texture references.  This is a hack
	
	static public String wallRef = "floorTile.png" ;
	static public String floorRef = "floorTile.png";
	Texture texture;
	
	public int x;
	public int y;
	public int cost;
	
	
	public int G; //cost of moving from start to here
	public int H; //cost of heuristic
	public int F; // G + H, for sorting
	
	public boolean opened;
	public boolean closed;
	public TileNode previous;
	
	public TerrainType type;

	
	public TileNode(){
		//TODO: ACTIVATED variable- know when active and when not 
		
		//this is initially a wall
		setType(TerrainType.wall);	
		
		
		previous = null;
		opened = false;
		closed = false;
		G = 0;//Integer.MAX_VALUE / 3;
		H = 0;//Integer.MAX_VALUE / 3;
		F = 0;//Integer.MAX_VALUE / 3;
	}
	
//	public void init(float x, float y){
//		position = new Vector3f(x,y,0.f);
//	}
	public void draw() {
		GL11.glPushMatrix();
		texture.bind();
		GL11.glTranslatef(x, y, 0);
		if (type == TerrainType.floor){
			GL11.glCallList(Ob.floorCL);
		}else if (type == TerrainType.wall){
			GL11.glCallList(Ob.wallCL);
		}
		//Ob.square.draw();
		//Ob.cube.draw(gl);
		GL11.glPopMatrix();

	}

	public int compareTo(TileNode another) {
		int ans = this.F -another.F;
		if (ans ==0){
			ans = this.H - another.H;
			//if (ans == 0){		
			//}
		}
		return ans;
	}
	public void setType(TerrainType iType){
		type = iType;
		//TODO: we should find another way of implementing this. Having TerrainType.wall.cost would be nice 
		if (type == TerrainType.wall){
			cost = 100;
			try {
				texture = Ob.textureLoader.getTexture(wallRef);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		if (type == TerrainType.floor){
			cost = 10;
			try {
				texture = Ob.textureLoader.getTexture(floorRef);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		
	}

}
